package com.hzy.admin.controller.core;

import com.hzy.admin.models.vo.system.AuthUserVo;
import com.hzy.admin.services.system.ISysUserService;
import com.hzy.framework.ApiResult;
import com.hzy.framework.controller.ViewController;
import com.hzy.framework.annotation.UserAuthorization;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 登陆 授权
 *
 * @author hzy
 */
@RequestMapping("/admin/login")
@Controller
@UserAuthorization(true)
public class LoginController extends ViewController {
    private final ISysUserService sysUserService;

    public LoginController(ISysUserService sysUserService) {
        this.sysUserService = sysUserService;
    }

    /**
     * 登录
     *
     * @return
     */
    @GetMapping()
    public String login() {
        this.sysUserService.setUserInfo(null);
        return this.view("login");
    }

    /**
     * 退出登录
     *
     * @return
     */
    @GetMapping("/out")
    public String logOut() {
        return this.viewRedirect("");
    }

    /**
     * 检查账户
     *
     * @param vo
     * @return
     */
    @PostMapping("/check")
    @ResponseBody
    public ApiResult check(@RequestBody AuthUserVo vo) {
        String token = this.sysUserService.check(vo);
        Map<String, Object> map = new HashMap<>();
        map.put("token", token);
        return this.resultOk(map);
    }

}
